package cn.edu.ctbu.datajpa01.controller;

import cn.edu.ctbu.datajpa01.domain.Achievement;
import cn.edu.ctbu.datajpa01.domain.Schedule;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.domain.Teacher;
import cn.edu.ctbu.datajpa01.service.AchievementService;
import cn.edu.ctbu.datajpa01.service.ScheduleService;
import cn.edu.ctbu.datajpa01.service.StudentService;
import cn.edu.ctbu.datajpa01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class StarterController {
    @Autowired
    StudentService studentService;

    @Autowired
    ScheduleService scheduleService;

    @Autowired
    AchievementService achievementService;

    @Autowired
    TeacherService teacherService;

    @RequestMapping("/starter/starter")
    public String list(HttpServletRequest httprequest , Model model){

        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //没有登录，需要去登录
            return "redirect:/login/user";
        }
        List<Student> students=studentService.findAll();

        model.addAttribute("data",students);
        return "/starter/starter" ;
    }


    // 跳转课程信息
    @RequestMapping("/starter/schedule")
    public String schedule(HttpServletRequest httprequest , Model model){

        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //没有登录，需要去登录
            return "redirect:/login/user";
        }
        List<Schedule> schedules=scheduleService.findAll();

        model.addAttribute("data",schedules);
        return "/starter/schedule" ;
    }

    // 跳转成绩界面
    @RequestMapping("/starter/achievement")
    public String achievement(HttpServletRequest httprequest , Model model){

        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //没有登录，需要去登录
            return "redirect:/login/user";
        }
        List<Achievement> achievements=achievementService.findAll();

        model.addAttribute("data",achievements);
        return "/starter/achievement" ;
    }

    // 跳转教师信息
    @RequestMapping("/starter/teacher")
    public String teacher(HttpServletRequest httprequest , Model model){

        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //没有登录，需要去登录
            return "redirect:/login/user";
        }
        List<Teacher> teachers=teacherService.findAll();

        model.addAttribute("data",teachers);
        return "/starter/teacher" ;
    }
}
